#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import shutil
from PIL import Image

def rename_files_in_directory(directory, old_substring, new_substring):
    """
    遍历指定目录下的所有文件并重命名，替换文件名中的特定子字符串。

    :param directory: 要遍历的目录路径
    :param old_substring: 要被替换的子字符串
    :param new_substring: 用于替换的新子字符串
    """
    # 获取目录下所有文件
    files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
    
    # 遍历文件并重命名
    for filename in files:
        # 构建旧文件的完整路径
        old_file_path = os.path.join(directory, filename)
        
        # 替换文件名中的子字符串
        #new_filename = filename.replace('.png', '.png.dat')
        new_filename = filename.replace(old_substring, new_substring)
        
        # 构建新文件的完整路径
        new_file_path = os.path.join(directory, new_filename)
        
        # 重命名文件
        shutil.move(old_file_path, new_file_path)
        print(f'Renamed: {old_file_path} -> {new_file_path}')
        # im0 = Image.open(new_file_path)
        # im0.show()


if __name__ == '__main__':
    directory = 'E:/YCS_Temp/project/thesis_dataset/VESSEL_Dataset'
    old_substring = 'im'
    new_substring = 'vessel_'
    rename_files_in_directory(directory, old_substring, new_substring)
